package com.hailiao.beidou.message.ui.page.chat.view

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.unit.dp
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.itemContentType
import androidx.paging.compose.itemKey
import com.hailiao.beidou.message.data.db.entity.MessageEntity
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.filter

/**
 * 消息列表
 * @Author d10ng
 * @Date 2023/11/22 11:32
 */
@OptIn(FlowPreview::class)
@Composable
fun ColumnScope.MessageListBox(
    initPosition: Int,
    name: String,
    messages: LazyPagingItems<MessageEntity>,
    onClickMessageLocation: (MessageEntity.Location) -> Unit = {}
) {
    val listStatus = rememberLazyListState()

    LaunchedEffect(initPosition) {
        // 滚动到指定位置
        if (initPosition > 0) {
            listStatus.scrollToItem(initPosition)
        }
    }
    // 虚拟键盘控制器
    val keyboardController = LocalSoftwareKeyboardController.current

    LaunchedEffect(listStatus) {
        snapshotFlow { listStatus.isScrollInProgress }
            .filter { it } // 当开始滚动时
            .collect { keyboardController?.hide() } // 隐藏软键盘
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .weight(1f)
            .pointerInput(Unit) {
                detectTapGestures(
                    onTap = {
                        // 点击空白处隐藏键盘
                        keyboardController?.hide()
                    }
                )
            }
    ) {
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth(),
            state = listStatus,
            reverseLayout = true,
            verticalArrangement = Arrangement.Bottom,
        ) {
            items(
                count = messages.itemCount,
                key = messages.itemKey(),
                contentType = messages.itemContentType()
            ) { index ->
                val item = messages[index]
                if (item != null) {
                    MessageView(
                        name = name,
                        message = item,
                        onClickLocation = { onClickMessageLocation(item.location!!) }
                    )
                    if (index == messages.itemCount - 1) {
                        Box(modifier = Modifier.height(16.dp))
                    }
                }
            }
        }
    }
}